package com.snow.springboot.service;

import com.snow.springboot.dao.NcmsDao;
import com.snow.springboot.entity.NcmsExcelInfo;
import com.snow.springboot.entity.NcmsInfo;
import com.snow.springboot.task.ProducNcmsTask;
import com.snow.springboot.util.MultiTaskPool;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Package: com.snow.springboot.service
 * @ClassName: NcmsService
 * @Description:新农合疾病数据 ES交互service
 * @author: lurui
 * @Date: 2018/10/24 下午2:12
 * @version:
 */
@Service
public class NcmsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(NcmsService.class);
    @Autowired
    private NcmsDao ncmsDao;
    @Autowired
    private ExcelService excelService;
    @Autowired
    private NcmsListener ncmsListener;

    /**
     * @Description: 通过ID查找疾病数据
     * @MethodName: getById
     * @Param: [id]
     * @Return: com.snow.springboot.entity.NcmsInfo
     * @Author: lurui
     * @Date: 2018/10/24 下午2:16
     */
    public NcmsInfo getById(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new IllegalArgumentException("疾病数据ID不能为空");
        }
        return this.ncmsDao.findById(id).get();
    }


    /**
     * @Description: 分条件查询ES数据
     * @MethodName: getListByParam
     * @Param: [param]
     * @Return: java.util.List<com.snow.springboot.entity.NcmsInfo>
     * @Author: lurui
     * @Date: 2018/10/24 下午2:25
     */
    public List<NcmsInfo> getListByParam(String param) {
        if (StringUtils.isEmpty(param)) {
            throw new IllegalArgumentException("疾病数据查询参数不能为空");
        }
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(param);
        Iterable<NcmsInfo> searchResult = ncmsDao.search(builder);
        Iterator<NcmsInfo> iterator = searchResult.iterator();
        List<NcmsInfo> list = new ArrayList<NcmsInfo>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }


    /**
     * @Description:保存新农合疾病信息
     * @MethodName: saveNcmsInfo
     * @Param: [ncmsInfo]
     * @Return: com.snow.springboot.entity.NcmsInfo
     * @Author: lurui
     * @Date: 2018/10/24 下午2:29
     */
    public NcmsInfo saveNcmsInfo(NcmsInfo ncmsInfo) {
        NcmsInfo result = ncmsDao.save(ncmsInfo);
        LOGGER.info("ncmsInfo:{}", result.getCode());
        return result;
    }

    /**
     * @Description: 根据ID删除ES中的数据信息
     * @MethodName: deleteNcmsInfo
     * @Param: [id]
     * @Return: void
     * @Author: lurui
     * @Date: 2018/10/24 下午2:33
     */
    public void deleteNcmsInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new IllegalArgumentException("疾病数据ID不能为空");
        }
        ncmsDao.deleteById(id);
    }

    /**
     * @Description:根据ID更新ES中疾病数据信息
     * @MethodName: updateNcmsInfo
     * @Param: [ncmsInfo]
     * @Return: com.snow.springboot.entity.NcmsInfo
     * @Author: lurui
     * @Date: 2018/10/24 下午2:36
     */
    public NcmsInfo updateNcmsInfo(NcmsInfo ncmsInfo) {
        if (StringUtils.isEmpty(ncmsInfo.getId())) {
            throw new IllegalArgumentException("疾病数据ID不能为空");
        }
        return this.saveNcmsInfo(ncmsInfo);
    }


    /**
     * @Description:
     * @MethodName: productNcmsService
     * @Param: [filePath, queue]
     * @Return: void
     * @Author: lurui
     * @Date: 2018/10/24 下午3:48
     */
    public void productNcmsService(String filePath, BlockingQueue<NcmsInfo> queue, MultiTaskPool multiTaskPool) {
        List<NcmsInfo> result = excelService.readFromFile(filePath, ncmsListener, NcmsExcelInfo.class);
        LOGGER.info("result size:{}", result.size());
        if (result != null && result.size() > 0) {
            for (int i = 0; i < result.size(); i++) {
                NcmsInfo ncmsInfo = result.get(i);
                multiTaskPool.executeJob(new ProducNcmsTask(ncmsInfo, queue));
            }
        }
    }


    /**
     * @Description:
     * @MethodName: consumeNcmsInfo
     * @Param: [queue, threadNum]
     * @Return: void
     * @Author: lurui
     * @Date: 2018/10/24 下午3:52
     */
    public void consumeNcmsInfo(BlockingQueue<NcmsInfo> queue, int threadNum, MultiTaskPool multiTaskPool) {
        /*for (int i = 0; i < threadNum; i++) {
            multiTaskPool.executeJob(new ConsumeNcmsTask(queue, this));
        }*/
        LOGGER.info("consume data!");
        NcmsInfo ncmsInfo = null;
        boolean flag = true;
        try {
            while (flag) {
                ncmsInfo = queue.poll(60, TimeUnit.SECONDS);
                if (ncmsInfo != null) {
                    this.saveNcmsInfo(ncmsInfo);
                } else {
                    flag = false;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            LOGGER.error("consumeNcms error.{}", ncmsInfo.getCode());
            Thread.currentThread().interrupt();
        }
    }


}
